﻿using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using Taype.Trading.BTC.Utilities;

namespace Taype.Trading.BTC.MtGoxAPIClient.JSONObjects
{
    /// <summary>
    /// This object encapsulates a trade on the MtGox echange
    /// {"date":1279408157,
    /// "price":"0.04951",
    /// "amount":"20",
    /// "price_int":"4951",
    /// "amount_int":"2000000000",
    /// "tid":"1",
    /// "price_currency":"USD",
    /// "item":"BTC",
    /// "trade_type":""
    /// "primary":"Y"
    /// }
    /// </summary>
    public class MtGoxTrade
    {
        public int date { get; set; }

        public double price { get; set; }

        public double amount { get; set; }

        public Int64 price_int { get; set; }

        public Int64 amount_int { get; set; }

        public string tid { get; set; }

        public MtGoxCurrencySymbol price_currency { get; set; }

        public string item { get; set; }

        public MtGoxTradeType trade_type { get; set; }

        public string primary { get; set; }

        /// <summary>
        /// Parses the JSON data returned by the 0/data/getTrades.php method
        /// </summary>
        /// <param name="jsonDataStr">JSON data returned by the 0/data/getTrades.php method</param>
        /// <returns></returns>
        public static List<MtGoxTrade> getObjects(string jsonDataStr)
        {
            List<MtGoxTrade> tradeList = new List<MtGoxTrade>();
            string json = jsonDataStr;
            var serializer = new JavaScriptSerializer();
            serializer.RegisterConverters(new[] { new DynamicJsonConverter() });
            dynamic obj = serializer.Deserialize(json, typeof(object));
            for (int i = 0; i < obj.Length; i++)
            {
                MtGoxTrade tr = new MtGoxTrade();
                // This object encapsulates b trade on the MtGox echange
                // {"date":1279408157,
                tr.date = obj[i].date;
                // "price":"0.04951",
                tr.price = Double.Parse(obj[i].price);
                // "amount":"20",
                tr.amount = Double.Parse(obj[i].amount);
                // "price_int":"4951",
                tr.price_int = Int64.Parse(obj[i].price_int);
                // "amount_int":"2000000000",
                tr.amount_int = Int64.Parse(obj[i].amount_int);
                // "tid":"1",
                tr.tid = obj[i].tid;
                // "price_currency":"USD",
                if (Enum.IsDefined(typeof(MtGoxCurrencySymbol), obj[i].price_currency))
                    tr.price_currency = (MtGoxCurrencySymbol)Enum.Parse(typeof(MtGoxCurrencySymbol), obj[i].price_currency, true);
                // "item":"BTC",
                tr.item = obj[i].item;
                // "trade_type":""
                if (Enum.IsDefined(typeof(MtGoxTradeType), obj[i].trade_type))
                    tr.trade_type = (MtGoxTradeType)Enum.Parse(typeof(MtGoxTradeType), obj[i].trade_type, true);
                // "primary":"Y"
                tr.primary = obj[i].primary;
                // }
                tradeList.Add(tr);
            }
            return tradeList;
        }
    }
}
